<?php


namespace app\admin\controller;


class AuthGroup extends \app\index\controller\Common
{
    public function lst(){
        $authGroupRes = \app\admin\model\AuthGroup::paginate(10);
        $page = $authGroupRes->render();
        $this->assign('page',$page);
        $this->assign('authGroupRes',$authGroupRes);
        return view('list');
    }
    public function add(){
        if(request()->isPost()){
            $data = input('post.');
            if(array_key_exists('rules',$data)){
                $data['rules'] = implode(',',$data['rules']);
            }
            $add = db('auth_group')->insert($data);
            if($add){
                $this->success('添加用户组成功',url('lst'));
            }else{
                $this->error('添加用户组失败');
            }
            return;
        }

        //获取所有的权限
        $authRuleRes = db('auth_rule')->select();
        $AuthRule = new \app\admin\model\AuthRule();
        $authRuleRes =  $AuthRule->ulCf($authRuleRes);
        $this->assign('authRuleRes',$authRuleRes);
        return view();
    }
    public function delete($id=0){
            $del = db('auth_group')->delete($id);
            if($del){
                $this->success('删除用户组成功',url('lst'));
            }else{
                $this->error('删除用户组失败',url('lst'));
            }
        }
    public function edit(){
        if(request()->isPost()){
            $data = input('post.');

            if(!array_key_exists('status', $data)){
                $data['status']=0;
            };
            if($data['rules']){
                $data['rules'] = implode(',',$data['rules']);
            }
            $save =db('auth_group')->where('id',$data['id'])->update($data);
            if($save!==false){
                $this->success('修改用户组成功',url('lst'));
            }else{
                $this->errot('修改用户组失败',url('lst'));
            }
        }
        //获取所有的权限
        $authRuleRes = db('auth_rule')->select();
        $AuthRule = new \app\admin\model\AuthRule();
        $authRuleRes =  $AuthRule->ulCf($authRuleRes);
        $this->assign('authRuleRes',$authRuleRes);
        $id = input('id');
        $authGroup = \app\admin\model\AuthGroup::find($id);
        $authGroup['rules'] = explode(',',$authGroup['rules']);
        $this->assign('authGroup',$authGroup);
        return view();
    }

}